﻿using Sustainalytics.News.BusinessLogic;
using Sustainalytics.News.Service.ErrorCodes;
using System.Linq;

namespace Sustainalytics.News.Service
{
    public static class ExceptionsExtensions
    {
        public static object GetHttpResponseMessageDto(this NewsFilterNotValidException ex)
        {
            return new
            {
                Code = (int)NewsErrorCodes.News_NewsFilterNotValid,

                ValidationErrors = ex.ValidationErrors == null ? null :
                    ex.ValidationErrors.Select<NewsFilterValidationException, object>(ex2 =>
                    {
                        if(ex2 is NewsFilterValidationCategoryNotValidException)
                        {
                            return new { Code = (int)NewsErrorCodes.News_NewsFilterNotValid_Criteria };
                        }
                        else
                        {
                            return null;
                        }
                    })
            };
        }

        public static object GetHttpResponseMessageDto(this NewsNotValidException ex)
        {
            return new
            {
                Code = (int)NewsErrorCodes.News_NewsNotValid,

                ValidationErrors = ex.ValidationErrors == null ? null : 
                    ex.ValidationErrors.Select<NewsValidationException, object>(ex2 => 
                    {
                        if (ex2 is NewsValidationAvailableForNotValidException)
                        {
                            return new { Code = (int)NewsErrorCodes.News_NewsNotValid_AvailableForNotValid };
                        }
                        else
                        {
                            if (ex2 is NewsValidationBodyNullOrEmptyException)
                            {
                                return new { Code = (int)NewsErrorCodes.News_NewsNotValid_BodyNullOrEmpty };
                            }
                            else
                            {
                                if (ex2 is NewsValidationCategoryNotValidException)
                                {
                                    return new { Code = (int)NewsErrorCodes.News_NewsNotValid_CategoryNotValid };
                                }
                                else
                                {
                                    if (ex2 is NewsValidationRegionNotValidException)
                                    {
                                        return new { Code = (int)NewsErrorCodes.News_NewsNotValid_RegionNotValid };
                                    }
                                    else
                                    {
                                        if (ex2 is NewsValidationTitleLengthException)
                                        {
                                            return new { Code = (int)NewsErrorCodes.News_NewsNotValid_TitleLength };
                                        }
                                        else
                                        {
                                            if (ex2 is NewsValidationTitleNullOrEmptyException)
                                            {
                                                return new { Code = (int)NewsErrorCodes.News_NewsNotValid_TitleNullOrEmpty };
                                            }
                                            else
                                            {
                                                return null;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }).ToArray()
            };
        }

        public static object GetHttpResponseMessageDto(this NewsDoesNotExistException ex)
        {
            return new { Code = (int)NewsErrorCodes.News_DoesNotExist };
        }

        public static object GetHttpResponseMessageDto(this NewsIdsNullException ex)
        {
            return new { Code = (int)NewsErrorCodes.News_NewsIdsNull };
        }

        public static object GetHttpResponseMessageDto(this NewsNullException ex)
        {
            return new { Code = (int)NewsErrorCodes.News_NewsIsNull };
        }

        public static object GetHttpResponseMessageDto(this NewsPatchDataNullException ex)
        {
            return new { Code = (int)NewsErrorCodes.News_NewsPatchDataNull };
        }

        public static object GetHttpResponseMessageDto(this PublishNonDraftNewsException ex)
        {
            return new { Code = (int)NewsErrorCodes.News_PublishNonDraftNews };
        }

        public static object GetHttpResponseMessageDto(this DeleteNonDraftNewsException ex)
        {
            return new { Code = (int)NewsErrorCodes.News_DeleteNonDraftNews };
        }
    }
}
